package com.itheima52.mobilesafe.activity;

import java.util.ArrayList;
import java.util.HashMap;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import com.itheima52.mobilesafe.R;

public class ContactActivity extends Activity {
	private ListView lvList;
	private ArrayList<HashMap<String, String>> readContact;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_contact);
		lvList = (ListView) findViewById(R.id.lv_list);
		readContact = readContact();
		System.out.println(readContact);
		lvList.setAdapter(new SimpleAdapter(ContactActivity.this, readContact,
				R.layout.contact_list_item, new String[] { "name", "phone" },
				new int[] { R.id.tv_name, R.id.tv_phone }));

		lvList.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				String phone = readContact.get(position).get("phone");// 读取当前item的电话号码
				Intent intent = new Intent();
				intent.putExtra("phone", phone);
				setResult(Activity.RESULT_OK, intent);// 将数据放在intent中返回给上个页面
				finish();
			}
		});

	}

	private ArrayList<HashMap<String, String>> readContact() {
		// 1.首先,从raw_contacts中读取联系人的id("contact_id")
		// 2.其次,根据contact_id中查找data表中查询出相应的电话号码和联系人名称
		// 3.然后,根据mimetype来区分哪个是联系人,哪个是电话号码

		// 1.首先,从raw_contacts中读取联系人的id("contact_id")
		Uri dataUri = Uri.parse("content://com.android.contacts/data");
		Uri rawContactUri = Uri
				.parse("content://com.android.contacts/raw_contacts");

		ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();

		/**
		 * uri查哪个表 projection查表中的哪个字段
		 */

		Cursor rawContactsCursor = getContentResolver().query(rawContactUri,
				new String[] { "contact_id" }, null, null, null);
		if (rawContactsCursor != null) {
			while (rawContactsCursor.moveToNext()) {
				String contactId = rawContactsCursor.getString(0);// 读第一列
				// System.out.println("contactId=" + contactId);
				// 2.其次,根据contact_id中查找data表中查询出相应的电话号码和联系人名称,实际上查询的是viw_data表
				// contact_id=?写成where contact_id=?会爆出查询的表
				Cursor dataCursor = getContentResolver().query(dataUri,
						new String[] { "data1", "mimetype" }, "contact_id=?",
						new String[] { contactId }, null);
				if (dataCursor != null) {
					HashMap<String, String> map = new HashMap<String, String>();
					while (dataCursor.moveToNext()) {
						String data1 = dataCursor.getString(0);
						String mimtype = dataCursor.getString(1);
//						System.out.println(contactId + ":" + data1 + ":"
//								+ mimtype);
						if ("vnd.android.cursor.item/phone_v2".equals(mimtype)) {
							map.put("phone", data1);
						} else if ("vnd.android.cursor.item/name"
								.equals(mimtype)) {
							map.put("name", data1);
						}

					}
					list.add(map);
					dataCursor.close();// 不关会影响性能

				}
			}
			rawContactsCursor.close();
		}
		return list;

	}

}
